package com.lrd.snapshotorder.config.redis;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;


/**
 * @author ：梁二东
 * @description：redisson 配置
 * @modified By：
 */
@Configuration
@ConditionalOnClass({Config.class, RedissonProperties.class})
public class RedissonConfig {

    @Autowired
    private RedissonProperties redssionProperties;


    /**
     * 单机模式自动装配
     *
     * @return
     */
    @Primary
    @Bean(name = "redissonSingle")
    @ConditionalOnProperty(name = "redisson.address")
    RedissonClient redissonSingle() {
        Config config = new Config();
        //配置序列化
        JsonJacksonCodec jsonJacksonCodec = new JsonJacksonCodec();
        config.setCodec(jsonJacksonCodec);
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress(redssionProperties.getAddress())
                .setTimeout(redssionProperties.getTimeout())
                .setConnectionPoolSize(redssionProperties.getConnectionPoolSize())
                .setConnectionMinimumIdleSize(redssionProperties.getConnectionMinimumIdleSize());

        if (StringUtils.isNotBlank(redssionProperties.getPassword())) {
            serverConfig.setPassword(redssionProperties.getPassword());
        }
        return Redisson.create(config);
    }

}
